<template>
    <div class="service-spec-select">
        <div class="price">
            <span class="currency">￥</span>
            <span class="price-text">{{ priceText }}</span>
        </div>
        <div class="service-num">
            <div class="spec-name">数量</div>
            <ElInputNumber v-model="serviceNum" :min="serviceNumRange[0]" :max="serviceNumRange[1]" size="small" />
        </div>
        <button
            class="purchase-btn"
            @click="confirmOrder"
        >
            立即购买
        </button>
        <!-- <ServicePurchaseConfirm :visible="modalVisible" :order="order" /> -->
    </div>
</template>

<script lang="ts" setup>
import { computed, Ref, ref, watch, PropType, watchEffect, onMounted, nextTick, onUpdated } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { Spec, SpecOption, Sku, SkuSelection } from '@/types/service.d';
// import ServicePurchaseConfirm from './ServicePurchaseConfirm.vue';
import { getOrderAdd, getOrderDelete } from '@/api/api.query.util'
import { ElMessage } from 'element-plus';
import { getCookie, debounce } from '@/utils/utils'

const userName = getCookie('userName')

interface ActiveSpec extends Spec {
    activeOptionId: number | undefined;
}

const props = defineProps({
    specs: {
        type: Array as PropType<Array<Spec>>,
        default: () => [],
    },
    price: {
        type: String,
    },
    skus: {
        type: Object,
        default: () => ({}),
    },
});

const router = useRouter();
const route = useRoute();
const priceText = ref('0')
const specStateList = ref([] as Array<ActiveSpec>);
const serviceNum: Ref<number> = ref(0);
const serviceNumRange = ref([1, 9999] as [number, number]);
const modalVisible = ref(false);
const order = ref({} as { [x: string]: any });
const locationSpecs: Ref<any> = ref([]);
onMounted(() => {
    if (route.query.serviceNum) {
        serviceNum.value = Number(route.query.serviceNum);
    }
    if (route.query.spec) {
        locationSpecs.value = route.query.spec;
    }
});

computed(() => {
    console.log('1asda11',props.skus)
})

watch(route.query.priceAll, () => {
    priceText.value = route.query.priceAll
})

watch(serviceNum, () => {
    console.log(props.skus)
    console.log()
    priceText.value = String(Number(props.skus.Price) * serviceNum.value);
});

watchEffect(()=>{
    console.log('gty',props.skus)
    if(route.query.priceAll) {
        priceText.value = route.query.priceAll
    } else {
        priceText.value = props.skus.Price
    }
})

/**
 * 规格选择处理函数
 * @param spec 当前规格
 * @param option 当前选项
 * @param disabled 是否可选
 */
function selectSpec(spec: Spec, option: SpecOption, disabled: Boolean) {
    if (disabled) {
        return;
    }

    const activeSelection = specStateList.value.find(({ specId }) => specId === spec.specId);
    if (activeSelection) {
        if (activeSelection.activeOptionId === option.optionId) {
            activeSelection.activeOptionId = undefined;
        } else {
            activeSelection.activeOptionId = option.optionId;
        }
    }
}

/**
 * 确认订单
 */
function confirmOrder() {
    if(route.query.orderId) {
        getOrderDelete({
        method: 'DELETE',
        data: {
            id : Number(route.query.orderId)
        },
    }).then((res) => {
    });
    }
    debounce(() => {
        getOrderAdd({
        method: 'post',
        data: {
            userName: userName,
            orderGoods: [{
                "GoodsName": props.skus.GoodsName,
                "price": props.skus.Price,
                "goodNum": serviceNum.value
            }]
        }
    }).then((res) => {
        if(res.code === 200) {
            ElMessage({
                        message: '创建订单成功',
                        type: 'success',
                    });
                    router.push( { name: '/home/my-orders' })
        }
    })
            }, 500);
    
}
</script>

<style scoped>
.price {
    color: var(--red-color);
}

.currency {
    font-size: 24px;
}

.price-text {
    font-size: 32px;
}

.price {
    margin-bottom: 50px;
}


.spec-name {
    font-size: var(--font-size-small);
    margin-bottom: 8px;
}

.option-list {
    display: flex;
    gap: 5px 16px;
    flex-wrap: wrap;
    flex: 0;
}

.option-list > li {
    height: 28px;
    line-height: 26px;
    max-width: 100%;
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color-transparent);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding: 0 15px;
    font-size: var(--font-size-small);
    cursor: pointer;
}
.option-list > li.active {
    border-color: var(--primary-color);
    background-color: var(--section-color);
    color: var(--font-color-link);
}

.option-list > li.disabled {
    color: var(--font-color-dim);
    border-color: transparent;
    background-color: var(--background-slight-transparent);
    cursor: not-allowed;
}

.service-num {
    margin-bottom: 22px;
}

.purchase-btn {
    width: 130px;
    height: 34px;
    background-color: var(--primary-color);
    border-radius: var(--border-radius);
    border: none;
    color: var(--font-color-reverse);
    cursor: pointer;
}
.purchase-btn.active {
    
}
</style>
